const express = require("express");
const { send, set } = require("express/lib/response");
//引入连接池模块
const pool = require("../pool");
//创建路由器对象
const r = express.Router();




// 搜索公司
r.get("/search_g", (req, res, next) => {
  // console.log(req.query);
  let obj = req.query.c_name;
  // console.log(obj);
  pool.query(
    `select * from job_companyintro where c_name like '%${obj}%'`,
    (err, r) => {
      if (err) {
        next(err);
        return;
      }
      res.send({
        code: 200,
        msg: "查询成功",
        data: r,
      });
    }
  );
});

// 后台公司分页
r.get("/fenye", (req, res) => {
  var page = parseInt(req.query.page); //页码 1 2 3 4 ...
  var pageSize = 6; //一页几行
  if (!page) {
    page = 1; //默认第一页
  }
  // console.log(page);
  var sql = "select * from job_companyintro limit ?,?"; //3:查询当前页内容
  var offset = (page - 1) * pageSize;
  pool.query(sql, [offset, pageSize], (err, result1) => {
    if (err) throw err;
    var sql = "select count(jid) as c from job_companyintro"; //查询总页数  4
    pool.query(sql, (err, result2) => {
      if (err) throw err;
      var count = Math.ceil(result2[0].c / pageSize); //1.25 =>2
      res.send({
        code: 1,
        msg: "成功",
        data: result1,
        total: count,
        pageNo: page,
      });
    });
  });
});

// 查询公司名  //注意查询时有字符串要加''
r.get("/gnames", (req, res, next) => {
  //执行SQL语句
  var obj = req.query;
  // console.log(obj)
  pool.query(
    "select c_name from job_companyintro where c_name=?",
    [obj.shan],
    (err, r) => {
      // console.log(r);
      if (err) {
        next(err);
        return;
      }
      if (r.length > 0) {
        res.send({ code: 200, msg: "用户名已存在" });
        return;
      } else {
        res.send({ code: 301, msg: "欢迎使用" });
        return;
      }
    }
  );
});
// 修改公司 之前
r.get("/sgsa", (req, res, next) => {
  //执行SQL语句
  var obj = req.query;
  // console.log(obj)
  pool.query(
    "select * from job_companyintro where jid=?",
    [obj.sgsa],
    (err, r) => {
      // console.log(r);
      if (err) {
        next(err);
        return;
      }
      if (r.length > 0) {
        res.send({ code: 200, msg: "查询成功" ,res:r});
        return;
      } else {
        res.send({ code: 301, msg: "查询失败" });
        return;
      }
    }
  );
});
// 修改公司
r.post("/sgsb", (req, res, next) => {
  var obj = req.body;
  // console.log(obj)



  pool.query(
    // c_name=?,c_merit1=?,c_type=?,c_xingzhi=?,c_number=?,c_address=? 
    "update job_companyintro set c_name=?,c_merit1=?,c_type=?,c_xingzhi=?,c_number=?,c_address=? where jid=?",
    [obj.c_name, obj.c_merit1, obj.c_type, obj.c_xingzhi,obj.c_number,obj.c_address,obj.jid],
    (err, r) => {
      if (err) {
        //抛出错误，阻止后边程序的执行，用于调试
        //throw err;
        //交给下一个错误处理中间件同时把错误信息传递过去
        next(err);
        //阻止往后执行
        return;
      }
      // console.log(r);
      if (r.affectedRows == 1) {
        res.send({ code: 200, msg: "修改成功" });
      } else {
        res.send({ code: 404, msg: "注册失败" });
      }
    }
  );
});


// 删除公司
r.get("/sgnames", (req, res, next) => {
  //执行SQL语句
  var obj = req.query;
  // console.log(obj.shan)
  // console.log(obj)
  pool.query(
    "delete from job_companyintro where jid=?",
    [obj.shan],
    (err, r) => {
      // console.log(r);
      if (err) {
        next(err);
        return;
      }
      // console.log(r)
      if (r.affectedRows == 1) {
        res.send({ code: 200, msg: "删除成功" });
        return;
      } else {
        res.send({ code: 301, msg: "删除失败" });
        return;
      }
    }
  );
});

// 添加公司
// c_name公司名称  c_merit1公司优点 c_type公司类型
// c_xingzhi公司性质 c_number公司人数 c_address公司地址
r.post("/tgnames", (req, res, next) => {
  var obj = req.body;
  // console.log(obj.tgsgm)
  pool.query(
    "insert job_companyintro(c_name,c_merit1,c_type,c_xingzhi,c_number,c_address) values(?,?,?,?,?,?)",
    [obj.tgsname, obj.tgsyd, obj.tgslx, obj.tgsxz,obj.tgsgm,obj.tgsdz],
    (err, r) => {
      if (err) {
        //抛出错误，阻止后边程序的执行，用于调试
        //throw err;
        //交给下一个错误处理中间件同时把错误信息传递过去
        next(err);
        //阻止往后执行
        return;
      }
      // console.log(r);
      if (r.affectedRows == 1) {
        res.send({ code: 200, msg: "注册成功" });
      } else {
        res.send({ code: 404, msg: "注册失败" });
      }
    }
  );
});









//往路由器下挂载路由
//首页轮播图下面
//接口地址：http://127.0.0.1:3000/v1/news/firm
r.get("/firm", (req, res, next) => {
  //执行SQL命令
  pool.query("select * from j_index_firm", (err, r) => {
    if (err) {
      // 抛给错误处理的中间件
      next(err);
      // 阻止往后执行
      return;
    }
    res.send({ code: 200, msg: "查询成功", data: r });
  });
});
// 删除首页f3楼的数据
//接口地址：http://127.0.0.1:3000/v1/news/delejob
r.delete("/delejob", (req, res, next) => {
  //执行SQL命令
  // console.log(req.query);
  let obj = req.query.jid;
  // console.log(obj);
  pool.query("delete  from index_job where jid=?", [obj], (err, r) => {
    if (r.affectedRows > 0) {
      res.send({ code: 200, msg: "删除成功" });
    } else {
      res.send({ code: 404, msg: "删除失败" });
    }
  });
});

// 获取首页f3楼的数据
//接口地址：http://127.0.0.1:3000/v1/news/job
r.get("/job", (req, res, next) => {
  //执行SQL命令
  pool.query("select * from index_job", (err, r) => {
    if (err) {
      // 抛给错误处理的中间件
      next(err);
      // 阻止往后执行
      return;
    }
    res.send({ code: 200, msg: "查询成功", data: r });
  });
});

//通过pid查询详情页面
//接口地址：http://127.0.0.1:3000/v1/news/details
r.get("/details", (req, res, next) => {
  obj = req.query;
  // console.log(obj);
  //执行SQL命令
  pool.query("select * from job_details where j_id=?", [obj.j_id], (err, r) => {
    if (err) {
      // 抛给错误处理的中间件
      next(err);
      // 阻止往后执行
      return;
    }
    // console.log(r);
    res.send({ code: 200, msg: "找到了成功", data: r });
  });
});
// http://127.0.0.1:3000/v1/news/companyintro
r.get("/companyintro", (req, res, next) => {
  obj = req.query;
  //执行SQL命令
  pool.query(
    "select * from job_companyintro where j_id=?",
    [obj.j_id],
    (err, r) => {
      if (err) {
        // 抛给错误处理的中间件
        next(err);
        // 阻止往后执行
        return;
      } else {
        pool.query(
          "select * from job_express where j_id=?",
          [obj.j_id],
          (err, r1) => {
            if (err) {
              // 抛给错误处理的中间件
              next(err);
              // 阻止往后执行
              return;
            } else {
              pool.query(
                "select * from job_details where j_id=?",
                [obj.j_id],
                (err, r2) => {
                  if (err) {
                    // 抛给错误处理的中间件
                    next(err);
                    // 阻止往后执行
                    return;
                  } else {
                    res.send({
                      code: 200,
                      msg: "查询成功",
                      data: r,
                      dataone: r1,
                      datatwo: r2,
                    });
                  }
                }
              );
            }
          }
        );
      }
    }
  );
});

// -------------
// http://127.0.0.1:3000/v1/news/resume
r.get("/resume", (req, res, next) => {
  obj = req.query;
  // console.log(obj);
  //执行SQL命令
  pool.query("select * from r_items where uid=?", [obj.uid], (err, r) => {
    if (err) {
      // 抛给错误处理的中间件
      next(err);
      // 阻止往后执行
      return;
    } else {
      pool.query(
        "select * from r_message where uid=?",
        [obj.uid],
        (err, r1) => {
          // console.log(r, r1);
          if (err) {
            // 抛给错误处理的中间件
            next(err);
            // 阻止往后执行
            return;
          } else {
            res.send({ code: 200, msg: "查询成功", data: r, data1: r1 });
          }

          // console.log(r);
        }
      );
    }

    // console.log(r);
  });
});
// --------------------
r.get("/products_q", (req, res) => {
  var page = parseInt(req.query.page); //页码 1 2 3 4 ...
  var pageSize = 6; //一页几行
  if (!page) {
    page = 1; //默认第一页
  }
  //3:查询当前页内容
  var sql = "select * from index_job limit ?,?";
  var offset = (page - 1) * pageSize;
  pool.query(sql, [offset, pageSize], (err, result1) => {
    if (err) throw err;
    //查询总页数  4
    var sql = "select count(jid) as c from index_job";
    pool.query(sql, (err, result2) => {
      if (err) throw err;
      var count = Math.ceil(result2[0].c / pageSize); //1.25 =>2
      //                          当前页内容     总页数      当前页码
      res.send({
        code: 1,
        msg: "成功",
        data: result1,
        total: count,
        pageNo: page,
      });
    });
  });
});
// --------------------------------

//http://127.0.0.1:3000/v1/news/products
// 可以传递页码出现想要的页面
r.get("/products", (req, res) => {
  var page = parseInt(req.query.page); //页码 1 2 3 4 ...
  var pageSize = 8; //一页几行
  if (!page) {
    page = 1; //默认第一页
  }
  //3:查询当前页内容
  var sql = "select * from index_job limit ?,?";
  var offset = (page - 1) * pageSize;
  pool.query(sql, [offset, pageSize], (err, result1) => {
    if (err) throw err;
    //查询总页数  4
    var sql = "select count(pid) as c from index_product";
    pool.query(sql, (err, result2) => {
      if (err) throw err;
      var count = Math.ceil(result2[0].c / pageSize); //1.25 =>2
      //                          当前页内容     总页数      当前页码
      res.send({
        code: 1,
        msg: "成功",
        data: result1,
        total: count,
        pageNo: page,
      });
    });
  });
});

// 加入购物车
//接口的地址：http://127.0.0.1:3000/v1/news/addgo
//接口的请求方式:post
r.post("/addgo", (req, res, next) => {
  var obj = req.body;
  // console.log(obj);
  pool.query("insert into shopping_cart set?", [obj], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (r.affectedRows == 1) {
      res.send({ code: 200, msg: "添加成功" });
    } else {
      res.send({ code: 404, msg: "添加失败" });
    }
  });
});

// 根据用户ID查询购物车
//接口地址：http://127.0.0.1:3000/v1/news/abount
r.get("/abount", (req, res, next) => {
  var obj = req.query;
  pool.query(
    "SELECT * FROM user,shopping_cart WHERE uid=? AND user_id=?",
    [obj.uid, obj.uid],
    (err, r) => {
      if (err) {
        next(err);
        return;
      } else if (!r.length == 0) {
        pool.query(
          "SELECT * FROM flower,shopping_cart WHERE lid=? AND product_id=?",
          [r[0].product_id, r[0].product_id],
          (err1, r1) => {
            if (err1) {
              next(err1);
              return;
            } else {
              // console.log(r1);
              res.send({
                code: 200,
                msg: "成功",
                data: r1,
                seruname: r,
              });
            }
          }
        );
      } else {
        res.send({ code: 404, msg: "查询失败" });
      }
    }
  );
});

// 搜索框
r.get("/search", (req, res, next) => {
  let obj = req.query;
  // console.log(obj.jtitle);
  pool.query(
    `SELECT * FROM index_job WHERE jtitle LIKE '%${obj.jtitle}%'`,
    (err, r) => {
      if (err) {
        next(err);
        return;
      }
      //如果没有错误，响应给前端
      res.send({ code: 200, msg: "查询成功", data: r });
    }
  );
  // console.log(obj.name);
});

//导出路由器
module.exports = r;
